using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using AMStudio.Common;

namespace AMStudio.UserInterface
{
    public partial class LoginForm : AWinFormShell
    {
        //private string user;

        //public string User
        //{
        //    get { return user; }
        //}

        //private PassportAccount mypassports;
        public LoginForm()
        {
            //this.mypassports = passports;
            InitializeComponent();
            this.UIName = this.Name;
        }

        private void LoginForm_Load(object sender, EventArgs e)
        {
            foreach (string k in UserDB.UserTags.Keys)
            {
                this.cmbUserName.Items.Add(k);
            }
            this.cmbUserName.Text = "Root";
        }
        public void ConfigrationUI(string softwareName, string softwareVision, string organization, string phone, string website)
        {
            this.Text = softwareName + "-Login";
            this.lblSoftwareName.Text = softwareName;
            this.lblSoftwareVision.Text = softwareVision;
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            string user = this.cmbUserName.Text.Trim();
            if (UserDB.UserTags.ContainsKey(user))
            {
                User current = UserDB.UserTags[user];
                if (current.Password == this.txtPassword.Text.Trim())
                {
                    UserDB.CurrentLoginUser = current;
                    AWinFormShell.LanguageString = cmbLanguage.Text;
                    AWinFormModule.LanguageString = cmbLanguage.Text;
                    this.DialogResult = DialogResult.OK;
                }
                else
                {
                    AWinFormShell.LanguageString = oldLanguageString;
                    AWinFormModule.LanguageString = oldLanguageString;
                }
            }
            else
            {
                AWinFormShell.LanguageString = oldLanguageString;
                AWinFormModule.LanguageString = oldLanguageString;
            }

        }


        public override bool ActivateUI()
        {
            oldLanguageString = LanguageString;
            this.cmbLanguage.Text=LanguageString;    
            base.ActivateUI();            
            if (this.ShowDialog() == DialogResult.OK)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        private void cmbUserName_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(cmbUserName.Text))
            {
                string user = this.cmbUserName.Text.Trim();
                if (UserDB.UserTags.ContainsKey(user))
                {
                    User current = UserDB.UserTags[user];
                    this.cmbLanguage.Text = current.Language;
                }         
            }
        }

        private string oldLanguageString = string.Empty;
        
        private void cmbLanguage_SelectedIndexChanged(object sender, EventArgs e)
        {
            oldLanguageString = LanguageString;
            LanguageString = this.cmbLanguage.Text;
            Language_InitializeControl(this);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            AWinFormShell.LanguageString = oldLanguageString;
            AWinFormModule.LanguageString = oldLanguageString;
        }
    }
}